% mk_tol_CW
% Chris Whalen
% University of Illinois
% 06/17/05 - Date Created
% 06/20/06 - Last Modified

% Purpose - to generate a TOL (talaraich optode locations) 

function mk_tol(choice,tai,xyz,opt_label,num_dum_pts,src,det,file)

tai.xyz = rai2lpi(tai.xyz);
xyz_tai = rai2lpi(xyz); % output matrix of xyz coords in talairach LPI coords
xyz_tai = xyz_tai - repmat(tai.xyz(1,:), size(xyz_tai,1), 1);
tai.xyz = tai.xyz - repmat(tai.xyz(1,:), size(tai.xyz,1), 1); % Placing in tai cords with origin at AC
[xyz_tai] =  TaiTfm (tai, xyz_tai); % Talairach Transforming xyz coords
if ((choice.align == 2) | (choice.align == 3)) & strcmpi(choice.rm_dum,'n') % Must not have removed dum points before 
    fprintf('\nRemoving Dummy Points For TOL File ... \n ');
    [opt_label,xyz_tai,num_dum_pts]=rm_dum_pts(src,det,opt_label,xyz_tai); % Remove Dummy Points from opt_label and xyz_tai
elseif num_dum_pts ~= 0
    fprintf('\nNumber of Dummy Points Removed: %d\n\n',num_dum_pts);
end
if choice.align ~= 1
    fprintf('Saving Talairach Transformed Data as ... %s \n',file.tol);
    fid = fopen(file.tol, 'w');
    for i=4:size(xyz_tai,1) 
        fprintf(fid,'%-s\t%f\t%f\t%f\r\n', opt_label{i}, xyz_tai(i,1), xyz_tai(i,2), xyz_tai(i,3)); 
    end
    if size(xyz_tai,1)~=size(opt_label,1) | size(xyz_tai,1)<=3
        fprintf('\nError: TOL file is blank or contains a dimension mismatch between labels and coordinates.\nPossible causes: MTG file in incorrect format\n');
    end
    fclose(fid);        
end




